package plugins.lxfa.puzzelGame
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import core.manager.MainSystemProperty;
	import core.manager.MainSystemPropertyEvent;
	import core.manager.pluginManager.PluginManager;
	import core.manager.sceneManager.SceneChangeEvent;
	import core.manager.sceneManager.SceneManager;
	
	import flash.events.ProgressEvent;
	
	import memory.MemoryRecovery;
	import memory.MyGC;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	
	import plugins.lsd.SlidingPuzzle.SlidingPuzzleGame;
	import plugins.lxfa.animatePlayer.AnimatePlayer;
	
	import view.loadings.LoadingWaveRota;
	
	public class PuzzelGameBase extends UIComponent
	{
		private var ui:NormalWindowSwc=new NormalWindowSwc();
		private var imageLoader:BulkLoader;
		private var loadingMc:LoadingWaveRota;
		private var game:SlidingPuzzleGame;
		public function PuzzelGameBase()
		{
			initLoader();
		}
		private function initLoadingMc():void
		{
			loadingMc=new LoadingWaveRota();
			loadingMc.x=Application.application.width/2;
			loadingMc.y=Application.application.height/2;
			Application.application.addChild(loadingMc);
		}
		/**
		 * 初始化loader
		 */ 
		private function initLoader():void
		{
			imageLoader=new BulkLoader("imageLoader");
			imageLoader.add("image/puzzelGame.jpg");
			imageLoader.add("point/img/19_1_1.jpg");
			imageLoader.start();
			imageLoader.addEventListener(BulkProgressEvent.COMPLETE,on_loaded);
			imageLoader.addEventListener(BulkProgressEvent.PROGRESS,updateProgress);
		}
		private function on_loaded(e:BulkProgressEvent):void
		{
			this.addChild(imageLoader.getBitmap("image/puzzelGame.jpg"));
            MemoryRecovery.getInstance().gcFun(imageLoader,BulkProgressEvent.COMPLETE,on_loaded);
            MemoryRecovery.getInstance().gcFun(imageLoader,BulkProgressEvent.PROGRESS,updateProgress);
            imageLoader.clear();
            imageLoader.removeAll();
            imageLoader = null;
            if(loadingMc!=null)
            {
            	if(loadingMc.parent!=null)
            	{
            		loadingMc.parent.removeChild(loadingMc);
            	}
            	loadingMc=null;
            }
            initContainer();
		}
		private function updateProgress(e:BulkProgressEvent):void
		{
			var event:ProgressEvent=new ProgressEvent(ProgressEvent.PROGRESS);
			event.bytesLoaded=e.bytesLoaded;
			event.bytesTotal=e.bytesTotal;
			if(loadingMc!=null)
			{
				loadingMc.updateByProgressEvent(event);
			}
		}
		/**
		 * 初始化容器
		 */ 
		private function initContainer():void
		{
			this.addChild(ui);
			ui.btn_360.visible=false;
			ui.Btn_Close.x=10000;
			ui.btn_picture.visible=false;
			ui.btn_video.visible=false;
			ui.btn_360_text.visible=false;
			ui.btn_picture_text.visible=false;
			ui.btn_video_text.visible=false;
			if(MainSystemProperty.LANGUAGE==MainSystemProperty.CHINESE)
			{
				ui.text.text="小游戏说明：移动鼠标，拼拼图，广西未来的瑰丽建筑与经济成就将在您手下渐渐呈现。";
			}else
			{
				ui.text.text="Specification to the game: move the mouse and arrange pictures. You will gradually see the magnificent buildings and splendid economic achievement of future Guangxi. ";
			}
			ui.x=35;ui.y=20;
			game=new SlidingPuzzleGame();
			ui.addChild(game);
			game.x=23;game.y=55;
			AnimatePlayer.getInstance().showAnimate(55);
			SceneManager.getInstance().dispacherJustBeforeCompleteEvent(12);
			SceneManager.getInstance().addEventListener(SceneChangeEvent.INIT,on_other_scene_init,false,0,true);
			SceneManager.getInstance().addEventListener(SceneChangeEvent.COMPLETE,on_other_scene_complete,false,0,true);
			MainSystemProperty.getInstance().addEventListener(MainSystemPropertyEvent.CHANGE_LANGUAGE,change_language,false,0,true);
		}
		private function change_language(e:MainSystemPropertyEvent):void{
			if(MainSystemProperty.LANGUAGE==MainSystemProperty.CHINESE)
			{
				if(ui!=null && ui.text!=null) ui.text.text="小游戏说明：移动鼠标，拼拼图，广西未来的瑰丽建筑与经济成就将在您手下渐渐呈现。";
			}else
			{
				if(ui!=null && ui.text!=null) ui.text.text="Specification to the game: move the mouse and arrange pictures. You will gradually see the magnificent buildings and splendid economic achievement of future Guangxi. ";
			}
		}
		private function on_other_scene_init(e:SceneChangeEvent):void
		{
			if(game!=null)
			{
				game.dispose();
				if(game.parent!=null)
				{
					game.parent.removeChild(game);
				}
				game=null;
			}
		}
		private function on_other_scene_complete(e:SceneChangeEvent):void
		{
			PluginManager.getInstance().removePluginById("PuzzelGameModule");
		}
		/**
		 * 销毁
		 */ 
		public function dispose():void
		{
            MemoryRecovery.getInstance().gcFun(imageLoader,BulkProgressEvent.COMPLETE,on_loaded);
            MemoryRecovery.getInstance().gcFun(imageLoader,BulkProgressEvent.PROGRESS,updateProgress);
            MemoryRecovery.getInstance().gcFun(SceneManager.getInstance(),SceneChangeEvent.INIT,on_other_scene_init);
            MemoryRecovery.getInstance().gcFun(SceneManager.getInstance(),SceneChangeEvent.COMPLETE,on_other_scene_complete);
            MemoryRecovery.getInstance().gcFun(MainSystemProperty.getInstance(),MainSystemPropertyEvent.CHANGE_LANGUAGE,change_language);
			AnimatePlayer.getInstance().removeAnimate();
			if(this.imageLoader!=null)
			{
	            imageLoader.clear();
	            imageLoader.removeAll();
	            imageLoader = null;
			}
			if(ui!=null)
			{
				if(ui.parent!=null)
				{
					ui.parent.removeChild(ui);
				}
				ui=null;
			}
			if(game!=null)
			{
				game.dispose();
				if(game.parent!=null)
				{
					game.parent.removeChild(game);
				}
				game=null;
			}
            if(loadingMc!=null)
            {
            	if(loadingMc.parent!=null)
            	{
            		loadingMc.parent.removeChild(loadingMc);
            	}
            	loadingMc=null;
            }
			MyGC.gc();
		}
	}
}